package example.borrow;

import example.entity.Borrow;
import example.entity.User;
import example.service.BorrowService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 张心怡
 * @date 2024/12/16
 */
@WebServlet("/searchBorrowServlet")
public class SearchBorrowServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前借阅 根据类型判断是不是管理员，用户智能看到自己的借阅记录
        String keyword = request.getParameter("searchQuery");
        Integer id;
        id = keyword == null ? null : Integer.parseInt(keyword);
        User user = (User) request.getSession().getAttribute("user");
        List<Borrow> records = null;
        if (user.getType() == 0) {
            records = BorrowService.searchBorrows(id, user.getId());
        } else {
            records = BorrowService.searchBorrows(id, null);
        }
        request.setAttribute("borrowRecords", records);
        request.getRequestDispatcher("manageBorrowRecords.jsp").forward(request, response);
    }
}
